/*------------------------------------------------------------------------------

Publication: Persson, M., Schakel, W. and Sundell, A. (2023) "A Man's World? The
	Policy Representation of Women and Men in a Comparative Perspective."
	Perspectives on Politics.

Description: This do-file runs the analyses presented in the results section of
	the paper, and also constructs two figures in the online appendix.

Sections:

	1. Main analysis
	2. Separate country analysis
	3. Appendix Figures A1-A2

Data: "persson_pop_data_01_main" (see "persson_schakel_sundell_pop_01_prep"
	do-file for - partial - construction)
	
	"persson_pop_data_02_national", based on data collected by Martin Gilens
	(United States), Lea Elsässer, Svenja Hense and Armin Schäfer (Germany),
	Mikael Persson (Sweden) and Wouter Schakel (Netherlands). Only Gilens' data
	are publicly available, at https://www.russellsage.org/datasets/economic-
	inequality-and-political-representation (downloaded in October 2021).

------------------------------------------------------------------------------*/

* This is where the datasets are saved
global data_main "YOUR\PATH\HERE"
global data_nat "YOUR\PATH\HERE"

/*------------------------------------------------------------------------------
 1. Main analysis
------------------------------------------------------------------------------*/

use "$data_main/persson_pop_data_01_main.dta", clear

* Table 1
ttest cong_women_dich == cong_men_dich
ttest cong_women_cont == cong_men_cont

* Here we show how many country-year-issues would have to be changed to equalize
* congruence (result mentioned in the text)
foreach v of varlist cong* {
	gen `v'_alt = `v'
}

gsort -cong_men_dich cong_women_dich
recode cong_men_dich_alt cong_women_dich_alt (0 = 1) (1 = 0) in 1/33
ttest cong_men_dich_alt == cong_women_dich_alt

gen congdiff = cong_women_cont - cong_men_cont
gsort -congdiff
replace cong_men_cont_alt = abs(1 - cong_men_cont_alt) in 1/136
replace cong_women_cont_alt = abs(1 - cong_women_cont_alt) in 1/136
ttest cong_men_cont_alt == cong_women_cont

drop cong*alt congdiff
sort country year source issue

* Table 2
reg polchange women_change men_change i.c i.year, vce(robust)
reg polchange diff i.c i.year, vce(robust)
reg polchange diff average_change i.c i.year, vce(robust)

* Predicted values for figure 3
margins, at(diff = (-0.1(0.01)0.1)) atmeans

* Appendix Table A1
tabstat year, stat(n min max) by(source) col(stat)

* Appendix Table A2
egen pickone = tag(issue)
bys polarea: egen temp1 = sum(pickone)
bys issue: gen temp2 = _N
table polarea, c(mean temp1 n c2)

* The issues listed as examples in Table A2 can be seen here
gsort polarea -temp2
egen pickone2 = tag(polarea)
list polarea issue if pickone2 == 1
drop pickone-pickone2
sort country year source issue

* Appendix Tables C1-C2 (with moderators standardized first)
foreach v of varlist femmin work_fem femparl culval turnout school_fem		///
	civillib civilsoc {
	egen `v'2 = std(`v')
	reg polchange i.c i.year average_change c.diff##c.`v'2, vce(cluster cy)
	drop `v'2
}

* The data in the "persson_pop_data_03_figures" Excel sheet - used to produce
* some figures in R, is based on the following commands.

* "fig 3"
reg polchange diff average_change i.c i.year, vce(robust)
margins, at(diff = (-.1(.01).1)) atmeans

* "year"
local c = 1
quietly foreach v of varlist school_fem femmin femparl work_fem culval		///
	turnout civillib civilsoc {
	egen `v'2 = std(`v')
	reg polchange c.diff##c.`v'2 i.year average_change, cluster(cy)
	matrix m`c'2 = r(table)
	matrix m`c' = m`c'2[1,3], m`c'2[2,3], m`c'2[1,3] - 1.645 * m`c'2[2,3],	///
	m`c'2[1,3] + 1.645 * m`c'2[2,3], m`c'2[1,3] - 1.96 * m`c'2[2,3],		///
	m`c'2[1,3] + 1.96 * m`c'2[2,3], 9 - `c'
	local ++c
	drop `v'2
}

matrix m_year = m1 \ m2 \ m3 \ m4 \ m5 \ m6 \ m7 \ m8 
matrix list m_year

* "year_country"
local c = 1
quietly foreach v of varlist school_fem femmin femparl work_fem culval		///
	turnout civillib civilsoc {
	egen `v'2 = std(`v')
	reg polchange c.diff##c.`v'2 i.c i.year average_change, cluster(cy)
	matrix m`c'2 = r(table)
	matrix m`c' = m`c'2[1,3], m`c'2[2,3], m`c'2[1,3] - 1.645 * m`c'2[2,3],	///
	m`c'2[1,3] + 1.645 * m`c'2[2,3], m`c'2[1,3] - 1.96 * m`c'2[2,3],		///
	m`c'2[1,3] + 1.96 * m`c'2[2,3], 9 - `c'
	local ++c
	drop `v'2
}

matrix m_year_country = m1 \ m2 \ m3 \ m4 \ m5 \ m6 \ m7 \ m8 
matrix list m_year_country

* "year_issue"
local c = 1
quietly foreach v of varlist school_fem femmin femparl work_fem culval		///
	turnout civillib civilsoc {
	egen `v'2 = std(`v')
	reg polchange c.diff##c.`v'2 i.year i.issue average_change, cluster(cy)
	matrix m`c'2 = r(table)
	matrix m`c' = m`c'2[1,3], m`c'2[2,3], m`c'2[1,3] - 1.645 * m`c'2[2,3],	///
	m`c'2[1,3] + 1.645 * m`c'2[2,3], m`c'2[1,3] - 1.96 * m`c'2[2,3],		///
	m`c'2[1,3] + 1.96 * m`c'2[2,3], 9 - `c'
	local ++c
	drop `v'2
}

matrix m_year_country = m1 \ m2 \ m3 \ m4 \ m5 \ m6 \ m7 \ m8 
matrix list m_year_country

* "year_country_issue"
local c = 1
quietly foreach v of varlist school_fem femmin femparl work_fem culval		///
	turnout civillib civilsoc {
	egen `v'2 = std(`v')
	reg polchange c.diff##c.`v'2 i.c i.year i.issue average_change, cluster(cy)
	matrix m`c'2 = r(table)
	matrix m`c' = m`c'2[1,3], m`c'2[2,3], m`c'2[1,3] - 1.645 * m`c'2[2,3],	///
	m`c'2[1,3] + 1.645 * m`c'2[2,3], m`c'2[1,3] - 1.96 * m`c'2[2,3],		///
	m`c'2[1,3] + 1.96 * m`c'2[2,3], 9 - `c'
	local ++c
	drop `v'2
}

matrix m_year_country_issue = m1 \ m2 \ m3 \ m4 \ m5 \ m6 \ m7 \ m8 
matrix list m_year_country_issue

/*------------------------------------------------------------------------------
 2. Separate country analysis
------------------------------------------------------------------------------*/

use "$data_main\persson_pop_data_02_national.dta", clear

* Creating the same measures of congruence as in the main data
gen cong_women_dich = (1 - round(abs(polchange - women_change), 1)) * 100
gen cong_men_dich = (1 - round(abs(polchange - men_change), 1)) * 100

gen cong_women_cont = (1 - abs(polchange - women_change)) * 100
gen cong_men_cont = (1 - abs(polchange - men_change)) * 100

* Creating average support and preference gaps
gen average_change = (men_change + women_change) / 2
gen diff = men_change - women_change

* Table 3
reg polchange men women if country == "US", vce(robust)
reg polchange men women if country == "DE", vce(robust)
reg polchange men women if country == "NL", vce(robust)
reg polchange men women if country == "SE", vce(robust)

* Appendix Table B1
ttest cong_men_dich == cong_women_dich if country == "US"
ttest cong_men_dich == cong_women_dich if country == "DE"
ttest cong_men_dich == cong_women_dich if country == "NL"
ttest cong_men_dich == cong_women_dich if country == "SE"

* Appendix Table B2
ttest cong_men_cont == cong_women_cont if country == "US"
ttest cong_men_cont == cong_women_cont if country == "DE"
ttest cong_men_cont == cong_women_cont if country == "NL"
ttest cong_men_cont == cong_women_cont if country == "SE"

* Appendix Table B3
reg polchange diff average_change if country == "US", vce(robust)
reg polchange diff average_change if country == "DE", vce(robust)
reg polchange diff average_change if country == "NL", vce(robust)
reg polchange diff average_change if country == "SE", vce(robust)

/*------------------------------------------------------------------------------
 3. Appendix Figures A1-A2
------------------------------------------------------------------------------*/

* For this, we once again use the main dataset
use "$data_main\persson_pop_data_01_main.dta", clear

* Appendix Figure A1
hist year, disc freq col(gs13) lc(gs9) lw(vvthin)							///
	xlab(1980(5)2015) ylab(0(100)400, ang(hor) glc(gs15))					///
	xtitle(Survey year, m(0 0 0 2)) ytitle(Frequency, m(0 2 0 0))			///
	plotr(m(2 2 0 0)) graphr(c(white) lc(white))

* Appendix Figure A2
bys c: gen count2 = _N
egen x = group(count2 country2)
labmask x, values(country2)

graph hbar (count), over(x, desc label(labs(*.9)))							///
	ysiz(10) bar(1, col(gs13) lc(gs11))										///
	ytitle(Frequency, m(0 0 0 2)) plotr(m(0 2 1 1))							///
	graphr(c(white) lc(white)) ylab(, glc(gs15))

drop count2 x
